$(function () {
  //渲染城市列表数据
  let cityArr = [
    [
      "沈阳",
      "天津",
      "长春",
      "太原",
      "乌鲁木齐",
      "兰州",
      "南京",
      "合肥",
      "长沙",
      "广州",
      "海口",
      "柳州",
    ],
    [
      "北京",
      "重庆",
      "呼和浩特",
      "西安",
      "拉萨",
      "银川",
      "武汉",
      "福州",
      "贵阳",
      "香港",
      "台北",
      "昆明",
      "黑龙江",
    ],
    [
      "上海",
      "哈尔滨",
      "石家庄",
      "济南",
      "西宁",
      "郑州",
      "杭州",
      "南昌",
      "成都",
      "南宁",
      "澳门",
      "成都",
    ],
  ];

  for (let i = 0; i < 3; i++) {
    $.each(cityArr[i], function (item, index) {
      $(".cityList")
        .eq(i)
        .append($(`<div class="cityNameList">` + index + `</div>`));
    });
  }

  //城市列表的显示和隐藏
  $(".moreCity").on("click", function () {
    $(".cityAdmintor").stop().show(400);
  });
  $(".cancel").click(function () {
    $(this).parent().parent().stop().hide(400);
  });

  // 跳转更多天气详情页面
  $(".getDetail").click(function () {
    location.href = "./detail.html?city=" + $(".cityName").text();
  });

  //获取url的值
  let url = window.location.href;
  //截取 url中的“=”,获得“=”后面的参数
  let getcity = url.split("=");
  //解码url
  getcity = decodeURI(getcity[1]); //decodeURI解码

  //根据城市名称渲染数据
  let getData = (theCity) => {
    $.ajax({
      type: "get",
      timeout: 5000,
      url: "http://api.tianapi.com/tianqi/index",
      data: {
        key: "60c783ccc05c6a72185d695b2aec2088",
        city: theCity,
      },
      success: function (response) {
        $(".cityName").text(response.newslist[0].area);
        $(".nowadaysDate").text(response.newslist[0].date);
        $(".nowadaysWeather").text(
          response.newslist[0].weather +
            "-" +
            response.newslist[0].lowest +
            "-" +
            response.newslist[0].highest
        );
        $(".tomorrowDetail").text(
          "明天：" +
            response.newslist[1].weather +
            "-" +
            response.newslist[1].lowest +
            "-" +
            response.newslist[1].highest
        );
        $(".tomorrowAfterDetail").text(
          "后天：" +
            response.newslist[2].weather +
            "-" +
            response.newslist[2].lowest +
            "-" +
            response.newslist[2].highest
        );
        $(".windDir").text("风向：" + response.newslist[0].wind);
        $(".terLow").text("最低温度：" + response.newslist[0].lowest);
        $(".windSbeed").text("湿度：" + response.newslist[0].humidity);
        $(".terHeight").text("最高温度：" + response.newslist[0].highest);
        $(".windIndex").text("风力：" + response.newslist[0].windsc);
        $(".rainfall").text("实际温度：" + response.newslist[0].real);
        $(".otherIndex").text(
          "紫外线强度指数：" + response.newslist[0].uv_index
        );
        $(".tip").text(response.newslist[0].tips);
      },
      error: function (e) {
        alert(e);
      }
    });
    // getcity="undefined";
  };

  console.log(getcity)
  let a = getcity == "undefined" ? "广州" : getcity;
  getData(a);
  // getData("北京");

  //页面刷新时调用
  // $(window).on("unload", function (event) {
  //   event.preventDefault()
  //   location.href = "./detail.html";
    // getcity="undefined";
    // let a = getcity == "undefined" ? "广州" : getcity;
    // getData("广州");
    // console.log(12345)
  // });

  //根据城市列表渲染数据
  $(".cityNameList").click(function () {
    getData($(this).text());
    $(".cityAdmintor").stop().hide(400);
  });
});
